package com.lxw.nonghe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lxw.nonghe.entity.Permission;
import com.lxw.nonghe.mapper.PermissionMapper;
import com.lxw.nonghe.service.PermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 权限表 服务实现类
 * </p>
 *
 * @author lxw
 * @since 2025-04-17
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService {

    @Override
    public Page<Permission> getPermissionPage(int current, int size, String permissionName) {
        Page<Permission> page = new Page<>(current,size);
        return (Page<Permission>) this.baseMapper.selectPermissionPage(page,permissionName);
    }

    @Override
    public Permission getPermissionByPermissionName(String permissionName) {
        return this.lambdaQuery().eq(Permission::getPermissionName,permissionName).one();
    }

    @Override
    public Page<Permission> queryPermission(Permission queryPermission, int current, int size) {
        LambdaQueryWrapper<Permission> wrapper = new LambdaQueryWrapper<>();
        if(queryPermission.getPermissionId() != null){
            wrapper.eq(Permission::getPermissionId, queryPermission.getPermissionId());
        }
        if(queryPermission.getPermissionName() != null){
            wrapper.eq(Permission::getPermissionName, queryPermission.getPermissionName());

        }
        if(queryPermission.getPermissionDesc() != null){
            wrapper.eq(Permission::getPermissionDesc, queryPermission.getPermissionDesc());
        }
        Page<Permission> page = new Page<>(current,size);
        return this.page(page,wrapper);
    }
    @Override
    public List<Permission> getPermissionTree() {
        // 查询所有权限
        List<Permission> allPermissions = this.list();

        // 构建树形结构
        return buildTree(allPermissions);
    }

    private List<Permission> buildTree(List<Permission> permissions) {
        List<Permission> tree = new ArrayList<>();

        // 找出所有一级权限
        Map<Integer, Permission> permissionMap = permissions.stream()
                .collect(Collectors.toMap(Permission::getPermissionId, Function.identity()));

        for (Permission permission : permissions) {
            if (permission.getParentId() == 0) {
                tree.add(permission);
            } else {
                Permission parent = permissionMap.get(permission.getParentId());
                if (parent != null) {
                    if (parent.getChildren() == null) {
                        parent.setChildren(new ArrayList<>());
                    }
                    parent.getChildren().add(permission);
                }
            }
        }

        return tree;
    }
}
